package jsyf.oa.service.system;

import com.alibaba.fastjson.JSONObject;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import jsyf.com.util.DateUtils;
import jsyf.com.util.FastJsonConvert;
import jsyf.oa.dao.system.ShouldSignHolidayDao;
import jsyf.oa.facade.system.ShouldSignHolidayFacade;

@Service
@Transactional(rollbackFor = Exception.class)
@com.alibaba.dubbo.config.annotation.Service(protocol = {"dubbo"})
@SuppressWarnings("all")
public class ShouldSignHolidayService implements ShouldSignHolidayFacade {
    @Autowired
    private ShouldSignHolidayDao shouldsignholidayDao;

    @Override
    public boolean config(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String chooseDates = jsonParams.getString("chooseDates");
        String frontType = jsonParams.getString("frontType");
        String nowMonth = jsonParams.getString("nowMonth");
        String userId = jsonParams.getString("userId");
        //删除一线岗位或后台岗位某年的应出勤天数配置
        shouldsignholidayDao.removeByCondition(" AND FRONT_FLAG = ? AND LEFT(SHOULD_DATE,7) = ?", new String[]{frontType, nowMonth});
        //已过去的天数
        List<String> arrayDayList = new ArrayList<>();
        //当前日期
        Calendar c = Calendar.getInstance();
        Integer lastDay = DateUtils.getLastDayOfMonth(Integer.valueOf(nowMonth.split("-")[0]),
                Integer.valueOf(nowMonth.split("-")[1]));
        for (Integer i = lastDay; i >= 1; i--) {
            arrayDayList.add(nowMonth + "-" + (i < 10 ? ("0" + i) : i));
        }
        if (StringUtils.isNotBlank(chooseDates)) {

            shouldsignholidayDao.insertBatch(arrayDayList, chooseDates, frontType, userId);
        }
        return true;
    }

    @Override
    public List<JSONObject> getShouldSignInList(String jsonObject) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonObject, JSONObject.class);
        String year = jsonParams.getString("year");
        if (StringUtils.isNotBlank(year)) {
            List<JSONObject> list = shouldsignholidayDao.getJSONListByCondition(" AND LEFT(SHOULD_DATE,4) = ? group by LEFT(SHOULD_DATE,7),FRONT_FLAG", new Object[]{year});
            return list;
        }
        return null;
    }

    @Override
    public List<JSONObject> getShouldSignMonthList(String jsonObject) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonObject, JSONObject.class);
        String year = jsonParams.getString("year");
        String month = jsonParams.getString("month");
        String frontType = jsonParams.getString("frontType");
        if (StringUtils.isNotBlank(year)) {
            List<JSONObject> list = shouldsignholidayDao.getShouldSignMonthList(year + "-" + month, frontType);
            return list;
        }
        return null;
    }


}
